<?php
	/**
	 * Страница поиска и исправления ошибок.
	 * 
	 * @author Костин Алексей Васильевич aka Volt(220)
	 * @copyright Copyright (c) 2010, Костин Алексей Васильевич
	 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public License
	 * @version 1.0
	 * @package HomeMoney
	 */

	/**
	 * Подключение настроек. 
	 */
	require_once("../ini.php");

	$DBCon=SQLDBFactory::getDB();

	$curTab=0;

	$sql="SELECT min(date) as С, max(date) as По FROM 
		(select date from prihod union select date from buys) as tab";
	$rez=$DBCon->select($sql, null, null, 1);
	$tabs[$curTab]=new TableTpl($rez);
	$tabs[$curTab]->title= "Промежуток времени";
	$curTab++;
	
	$sql="select id from buys where id not in (select id_buy from basket ) order by id";
	$rez=$DBCon->select($sql);
	$tabs[$curTab]=new TableTpl($rez);
	$tabs[$curTab]->title= "Потерянные покупки";
	$tabs[$curTab]->class= "overItem buys";
	$tabs[$curTab]->sumRows(0);
	$curTab++;

	$sql="Select id from basket where id_buy not in (select id from buys) order by id";
	$rez=$DBCon->select($sql);
	$tabs[$curTab]=new TableTpl($rez);
	$tabs[$curTab]->title= "Потерянные товары";
	$tabs[$curTab]->class= "overItem basket";
	$tabs[$curTab]->sumRows(0);
	$curTab++;

	$sql="select id, sum as `Сумма покупки`, t1.s as `Сумма товаров в покупке` from
		buys left outer join (select id_buy, sum(sum) as s from basket group by id_buy) as t1 on t1.id_buy=buys.id
		where sum<t1.s-1 or sum>t1.s+1 
		order by id";
	$rez=$DBCon->select($sql);
	$tabs[$curTab]=new TableTpl($rez);
	$tabs[$curTab]->title= "Несоответствия сумм";
	$curTab++;

	$sql="Select id, item as Товар from goods where id not in (select id_good from basket union select id_good from needToBuy) order by item";
	$rez=$DBCon->select($sql, null, null, 1);
	$tabs[$curTab]=new TableTpl($rez);
	$tabs[$curTab]->title= "Лишние наименования";
	$tabs[$curTab]->class= "overItem goods";
	$tabs[$curTab]->sumRows(0);
	$curTab++;

	$sql="SELECT item as Товар, cat as Категория, name as Магазин, addr as Адрес, col as Количество, price as Цена, basket.sum as `Сумма товара`, buys.id `id покупки`, buys.sum as `Сумма покупки`, round(sumB.sum,2) as `Сумма товаров`
		FROM basket
		LEFT OUTER JOIN goods ON goods.id = basket.id_good
		LEFT OUTER JOIN buys ON buys.id = basket.id_buy
		LEFT OUTER JOIN shops ON shops.id = buys.id_shop
		LEFT OUTER JOIN shops_address on shops_address.id_shop=shops.id
		LEFT OUTER JOIN address on address.id=shops_address.id_addr
		LEFT OUTER JOIN categ ON goods.id_cat = categ.id
		LEFT OUTER JOIN
		(select sum(sum) as sum, id_buy from basket group by id_buy)
		as sumB on sumB.id_buy=basket.id_buy
		order by date desc, goods.id";
	$rez=$DBCon->select($sql);
	$tabs[$curTab]=new TableTpl($rez);
	$tabs[$curTab]->title= "Полная статистика";
	$curTab++;

	$bodyTpl=new Template(DOCROOT."/Templates/tablesPrint.tpl");
	$bodyTpl->tables=$tabs;

	$pageT="Ошибки";
	include (DOCROOT."/pages.php"); 
?>